﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using WMSSyncData.DAL;
using WMSSyncData.DAL.Oracle;
using WMSSyncData.DAL.SqlDB;
using WMSSyncData.Model;
using WMSSyncData.Utils;

namespace WMSSyncData.Core
{
    public class WMSSyncRMToERP
    {
        /// <summary>
        /// 采购订单收货
        /// </summary>
        public void InStockSync()
        {
            Tddev102DAL tddev = new Tddev102DAL();
            WMS_T_InStorckRMRowDetailsDAL wMS_T_InStorckRMRowDetailsDAL = new WMS_T_InStorckRMRowDetailsDAL();

            var list = tddev.GetSyncData();

            if (list != null && list.Count > 0)
            {
                foreach (var r102 in list)
                {
                    var ret = new BaseOracleDAL<tddev102>().Insert(r102);
                    if (ret == -1 || ret > 0)
                    {
                        //***20210221合并仓库
                        SH_PO_Adjust(r102);
                        tddev.UpdateUploadStatus(r102);
                    }
                }
            }
        }

        /// <summary>
        /// 分包收货
        /// </summary>
        public void InStockFBSYnc()
        {
            Tddev122DAL tddev122 = new Tddev122DAL();

            var list = tddev122.GetFBAdjustmentData();
            if (list != null && list.Count > 0)
            {
                if (new BaseOracleDAL<tddev122>().InsertBatch(list) > 0)
                {
                    tddev122.UpdateUploadStatus(list);
                }
            }
        }

        /// <summary>
        /// 生产退料
        /// </summary>
        public void InStockPRSync()
        {
            Tddev106DAL tddev106DAL = new Tddev106DAL();
            WMS_T_InStorckRMRowDetailsDAL wMS_T_InStorckRMRowDetailsDAL = new WMS_T_InStorckRMRowDetailsDAL();

            //var ListTddev106 = tddev106DAL.GetSyncData();

            DataTable NGFGDt = tddev106DAL.GetSyncTLRKData();
            List<tddev106> list106 = new List<tddev106>();

            if (NGFGDt != null && NGFGDt.Rows.Count > 0)
            {
                foreach (DataRow dr in NGFGDt.Rows)
                {

                    if (Convert.ToDecimal(dr["FGQty"]) > 0)
                    {
                        tddev106 entityFG = new tddev106();
                        entityFG.guid = Guid.NewGuid().ToString();
                        entityFG.WMSNO = dr["BillNo"].ToString();
                        if (string.IsNullOrEmpty(dr["RelatedBill"].ToString()))
                        {
                            entityFG.orno = " ";
                        }
                        else
                        {
                            entityFG.orno = dr["RelatedBill"].ToString();
                        }
                        entityFG.pono = Convert.ToInt32(dr["RowNo"]);
                        entityFG.otyp = "TWH";
                        if (string.IsNullOrEmpty(dr["Ext1"].ToString()))
                        {
                            entityFG.sfco = " ";
                        }
                        else
                        {
                            entityFG.sfco = dr["Ext1"].ToString();
                        }

                        entityFG.sflo = " ";
                        entityFG.item = dr["PartNumber"].ToString(); ;
                        entityFG.stco = dr["FGSN"].ToString();   //入库仓库
                        entityFG.stlo = " ";
                        entityFG.qoor = Convert.ToDecimal(dr["FGQty"]);
                        entityFG.host = 1;
                        entityFG.mess = " ";
                        entityFG.edte1 = DateTime.Now;
                        entityFG.REFCNTU = 0;
                        entityFG.REFCNTD = 0;
                        entityFG.wdte1 = DateTime.Now;
                        entityFG.edte2 = DateTime.Now;
                        entityFG.seri = "WH5";
                        entityFG.revi = dr["Version"].ToString();
                        entityFG.Remark1 = " ";
                        entityFG.Remark2 = " ";
                        entityFG.Remark3 = " ";
                        entityFG.Remark4 = " ";
                        entityFG.Remark5 = " ";
                        list106.Add(entityFG);
                    }

                    if (Convert.ToDecimal(dr["NGQty"]) > 0)
                    {
                        tddev106 entityNG = new tddev106();
                        entityNG.guid = Guid.NewGuid().ToString();
                        entityNG.WMSNO = dr["BillNo"].ToString();
                        if (string.IsNullOrEmpty(dr["RelatedBill"].ToString()))
                        {
                            entityNG.orno = " ";
                        }
                        else
                        {
                            entityNG.orno = dr["RelatedBill"].ToString();
                        }
                        entityNG.pono = Convert.ToInt32(dr["RowNo"]);
                        entityNG.otyp = "TWH";
                        if (string.IsNullOrEmpty(dr["Ext1"].ToString()))
                        {
                            entityNG.sfco = " ";
                        }
                        else
                        {
                            entityNG.sfco = dr["Ext1"].ToString();
                        }
                        //entityNG.sfco = dr["Ext1"].ToString(); //出库仓库
                        entityNG.sflo = " ";
                        entityNG.item = dr["PartNumber"].ToString(); ;
                        entityNG.stco = dr["NGSN"].ToString();       //入库仓库
                        entityNG.stlo = " ";
                        entityNG.qoor = Convert.ToDecimal(dr["NGQty"]);
                        entityNG.host = 1;
                        entityNG.mess = " ";
                        entityNG.edte1 = DateTime.Now;
                        entityNG.REFCNTU = 0;
                        entityNG.REFCNTD = 0;
                        entityNG.wdte1 = DateTime.Now;
                        entityNG.edte2 = DateTime.Now;
                        entityNG.seri = "WH5";
                        entityNG.revi = dr["Version"].ToString();
                        entityNG.Remark1 = " ";
                        entityNG.Remark2 = " ";
                        entityNG.Remark3 = " ";
                        entityNG.Remark4 = " ";
                        entityNG.Remark5 = " ";
                        list106.Add(entityNG);
                    }
                }
            }

            //var updateList = wMS_T_InStorckRMRowDetailsDAL.GetPRUpdate();
            if (list106.Count > 0)
            {
                if (new BaseOracleDAL<tddev106>().InsertBatch(list106) > 0)
                {
                    //tddev106DAL.UpdateUploadFGNGStatus(NGFGDt);
                    tddev106DAL.CloseBackRMStatus(NGFGDt);
                }
            }
        }

        public void InStockPRBOMSync()
        {
            Tddev107DAL tddev107DAL = new Tddev107DAL();
            WMS_T_InStorckRMRowDetailsDAL wMS_T_InStorckRMRowDetailsDAL = new WMS_T_InStorckRMRowDetailsDAL();

            var ListTddev107 = tddev107DAL.GetSyncData();

            var updateList = wMS_T_InStorckRMRowDetailsDAL.GetPRBOMUpdate();
            if (ListTddev107.Count > 0)
            {
                if (new BaseOracleDAL<tddev107>().InsertBatch(ListTddev107) > 0)
                {
                    tddev107DAL.UpdateUploadStatus(updateList);
                }
            }
        }

        /// <summary>
        /// 生产领料
        /// </summary>
        public void OutStockPOMSync()
        {
            Tddev106DAL tddev106DAL = new Tddev106DAL();
            WMS_T_OutStorckRMRowDetailsDAL wMS_T_OutStorckRMRowDetailsDAL = new WMS_T_OutStorckRMRowDetailsDAL();
            WMS_M_OutStockRMDetailsDAL wMS_M_OutStockRMDetailsDAL = new WMS_M_OutStockRMDetailsDAL();
            var result = tddev106DAL.UpdateUploadPOMOStatus(); //修改需要同步的生产领料数据状态
                                                               //if (result)
            {
                var ListTddev106 = tddev106DAL.GetPOMSyncData(); //获取需要同步的生产领料数据
                var ListwMS_M_OSRMRD = wMS_M_OutStockRMDetailsDAL.GetPOMSyncData(); //获取更新到明细表数据
                var updateList = wMS_T_OutStorckRMRowDetailsDAL.GetPRUpdate(); //获取需要更新到本地的生产领料数据

                if (ListTddev106.Count > 0)
                {
                    int ret = 0;

                    if (ListwMS_M_OSRMRD.Count > 0)
                    {
                        ret = new BaseOracleDAL<WMS_M_OutStockRMDetails>().InsertSPBatch(ListwMS_M_OSRMRD);
                        if (ret == 0)
                        {
                            return;
                        }
                    }

                    if (new BaseOracleDAL<tddev106>().InsertBatch(ListTddev106) > 0) //同步
                    {
                        tddev106DAL.UpdateUploadPOMTStatus(updateList); //更新本地
                    }
                }
            }
        }

        public void OutStockPOMBOMSync()
        {

            Tddev102DAL tddev102DAL = new Tddev102DAL();
            WMS_T_OutStorckRMRowDetailsDAL wMS_T_OutStorckRMRowDetailsDAL = new WMS_T_OutStorckRMRowDetailsDAL();

            var listRows = tddev102DAL.GetSyncOutStockData();
            if (listRows != null && listRows.Count > 0)
            {
                if (new BaseOracleDAL<tddev102>().InsertBatch(listRows) > 0) //同步
                {
                    wMS_T_OutStorckRMRowDetailsDAL.UpdateOutStockRows(listRows); //更新本地
                }
            }
        }

        /// <summary>
        /// 同步检验数据
        /// </summary>
        public void IQCSync()
        {

            Tddev106DAL tddev106DAL = new Tddev106DAL();
            WMS_T_InStorckRMRowDetailsDAL wMS_T_InStorckRMRowDetailsDAL = new WMS_T_InStorckRMRowDetailsDAL();

            //var ListTddev106 = tddev106DAL.GetSyncData();

            DataTable NGFGDt = tddev106DAL.GetSyncRKData();
            List<tddev106> list106 = new List<tddev106>();

            if (NGFGDt != null && NGFGDt.Rows.Count > 0)
            {
                foreach (DataRow dr in NGFGDt.Rows)
                {

                    //if (Convert.ToDecimal(dr["FGQty"]) > 0)
                    //{
                    //    tddev106 entityFG = new tddev106();
                    //    entityFG.guid = Guid.NewGuid().ToString();
                    //    entityFG.WMSNO = dr["BillNo"].ToString();
                    //    if (string.IsNullOrEmpty(dr["RelatedBill"].ToString()))
                    //    {
                    //        entityFG.orno = " ";
                    //    }
                    //    else
                    //    {
                    //        entityFG.orno = dr["RelatedBill"].ToString();
                    //    }
                    //    entityFG.pono = Convert.ToInt32(dr["RowNo"]);
                    //    entityFG.otyp = "TWH";
                    //    if (string.IsNullOrEmpty(dr["Ext1"].ToString()))
                    //    {
                    //        entityFG.sfco = " ";
                    //    }
                    //    else
                    //    {
                    //        entityFG.sfco = dr["Ext1"].ToString();
                    //    }
                    //    entityFG.sflo = " ";
                    //    entityFG.item = dr["PartNumber"].ToString(); ;
                    //    entityFG.stco = dr["FGSN"].ToString();
                    //    entityFG.stlo = " ";
                    //    entityFG.qoor = Convert.ToDecimal(dr["FGQty"]);
                    //    entityFG.host = 1;
                    //    entityFG.mess = " ";
                    //    entityFG.edte1 = DateTime.Now;
                    //    entityFG.REFCNTU = 0;
                    //    entityFG.REFCNTD = 0;
                    //    entityFG.wdte1 = DateTime.Now;
                    //    entityFG.edte2 = DateTime.Now;
                    //    entityFG.seri = "WH5";
                    //    entityFG.revi = dr["Version"].ToString();
                    //    entityFG.Remark1 = " ";
                    //    entityFG.Remark2 = " ";
                    //    entityFG.Remark3 = " ";
                    //    entityFG.Remark4 = " ";
                    //    entityFG.Remark5 = " ";
                    //    list106.Add(entityFG);
                    //}

                    if (Convert.ToDecimal(dr["NGQty"]) > 0)
                    {
                        tddev106 entityNG = new tddev106();
                        entityNG.guid = Guid.NewGuid().ToString();
                        entityNG.WMSNO = dr["BillNo"].ToString();
                        if (string.IsNullOrEmpty(dr["RelatedBill"].ToString()))
                        {
                            entityNG.orno = " ";
                        }
                        else
                        {
                            entityNG.orno = dr["RelatedBill"].ToString();
                        }
                        entityNG.pono = Convert.ToInt32(dr["RowNo"]);
                        entityNG.otyp = "TWH";
                        if (string.IsNullOrEmpty(dr["FGSN"].ToString()))
                        {
                            entityNG.sfco = " ";
                        }
                        else
                        {
                            entityNG.sfco = dr["FGSN"].ToString();
                        }

                        entityNG.sflo = " ";
                        entityNG.item = dr["PartNumber"].ToString(); ;
                        entityNG.stco = dr["NGSN"].ToString();
                        entityNG.stlo = " ";
                        entityNG.qoor = Convert.ToDecimal(dr["NGQty"]);
                        entityNG.host = 1;
                        entityNG.mess = " ";
                        entityNG.edte1 = DateTime.Now;
                        entityNG.REFCNTU = 0;
                        entityNG.REFCNTD = 0;
                        entityNG.wdte1 = DateTime.Now;
                        entityNG.edte2 = DateTime.Now;
                        entityNG.seri = "WH5";
                        entityNG.revi = dr["Version"].ToString();
                        entityNG.Remark1 = " ";
                        entityNG.Remark2 = " ";
                        entityNG.Remark3 = " ";
                        //entityNG.Remark1 = dr["Ext1"].ToString();;
                        //entityNG.Remark2 = dr["NGSN"].ToString();
                        //entityNG.Remark3 = dr["Ext3"].ToString();
                        entityNG.Remark4 = " ";
                        entityNG.Remark5 = " ";
                        entityNG.epon = 0;
                        entityNG.seqn = 0;
                        list106.Add(entityNG);
                    }
                }

                //tddev106DAL.UpdateUploadRKStatus(NGFGDt);
                //分托补充
                tddev106DAL.UpdateUploadFTStatus(NGFGDt);
            }

            if (list106.Count > 0)
            {
                if (new BaseOracleDAL<tddev106>().InsertBatch(list106) > 0)
                {
                    //tddev106DAL.UpdateUploadRKStatus(NGFGDt);
                }
            }

        }

        /// <summary>
        /// 非生产领料
        /// </summary>
        public void RequestSync()
        {
            Tddev110DAL tddev110DAL = new Tddev110DAL();
            WMS_T_RequestDAL wMS_T_RequestDAL = new WMS_T_RequestDAL();

            var tddev110List = tddev110DAL.GetSyncData();
            //var RequList = wMS_T_RequestDAL.GetAllUpdate();
            if (tddev110List != null && tddev110List.Count > 0)
            {
                if (new BaseOracleDAL<tddev110>().InsertBatch(tddev110List) > 0)
                {
                    wMS_T_RequestDAL.UpdateUploadStatus(tddev110List);
                }
            }
        }


        public void PBackSync()
        {
            Tddev106DAL tddev106DAL = new Tddev106DAL();
            WMS_T_OutStorckRMRowDetailsDAL wMS_T_OutStorckRMRowDetailsDAL = new WMS_T_OutStorckRMRowDetailsDAL();
            WMS_M_OutStockRMDetailsDAL wMS_M_OutStockRMDetailsDAL = new WMS_M_OutStockRMDetailsDAL();

            var ListTddev106 = tddev106DAL.GetPBackSyncData(); //获取需要同步的生产领料数据

            var updateDt = wMS_T_OutStorckRMRowDetailsDAL.getPBackUpdate(); //获取需要更新到本地的生产领料数据
            if (ListTddev106.Count > 0)
            {
                if (new BaseOracleDAL<tddev106>().InsertBatch(ListTddev106) > 0) //同步
                {
                    tddev106DAL.UpdateUploadPBackStatus(updateDt); //更新本地
                }
            }

        }

        //分包V5951S海盐处理
        public void DoFBV5951S(List<tddev106> tddev106s)
        {
            //分包V5951H
            var dt = tddev106s.FindAll(x => x.stco == "V5951S");
            StringBuilder cmd = new StringBuilder();
            foreach (var row in dt)
            {
                string sql = "";
                sql = "select 'A0204' +  CONVERT(varchar(8), GETDATE(), 12)+right('00000'+ltrim(NEXT VALUE FOR SEQ_INSTOCK),5)";
                var billNo = SqlDBHelper.GetObject(sql).ToString();

                var item = row.item;
                var sql105 = @"select t.t$aitc from ttcibd004101 t where t$bpid = 'V5951-1' and t$item = '{0}'";
                sql105 = string.Format(sql105, row.item);
                var newItem = OracleDBHelper.ExecuteScalar(sql105);
                if (newItem != null)
                {
                    if (newItem.ToString().StartsWith("RD") == false)
                    {
                        item = "         " + newItem.ToString();
                    }
                    else
                    {
                        item = newItem.ToString();
                    }
                }

                //插入分包采购
                sql = @"INSERT INTO [dbo].[wms_T_InStockRM]
           ([BillNo]
           ,[BillType]
           ,[RelatedBill]
           ,[Supplier]
           ,[Storage_SN]
           ,[Status]
           ,[Remark]
           ,[Creator]
           ,[CreateTime]
           ,[Updator]
           ,[UpdateTime]
           ,[Ext1]
           ,[Ext2]
           ,[Ext3]
           ,[SupplierName]
           ,[Updator2]
           ,[UpdateTime2]
           ,[Updator3]
           ,[UpdateTime3]
           ,[Updator4]
           ,[UpdateTime4]
           ,[SHStorage_SN])
     VALUES
           ('{0}'
           ,'分包入库'
           ,'V5951S'
           ,'SAA'
           ,'RM'
           ,'0'
           ,null
           ,'admin'
           ,getdate()
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null);";
                sql = string.Format(sql, billNo);
                cmd.Append(sql);
                sql = @"INSERT INTO [dbo].[wms_T_InStockRMRows]
           ([BillNo]
           ,[RelatedBill]
           ,[PartNumber]
           ,[RowNo]
           ,[OrderNo]
           ,[PlanQty]
           ,[RealQty]
           ,[Unit]
           ,[Status]
           ,[Creator]
           ,[CreateTime]
           ,[Updator]
           ,[UpdateTime]
           ,[Ext1]
           ,[Ext2]
           ,[Ext3]
           ,[Version]
           ,[WaitRemark]
           ,[ToolRemark]
           ,[ASN]
           ,[LotNo])
     VALUES
           ('{0}'
           ,'V5951S'
           ,'{1}'
           ,1
           ,1
           ,{2}
           ,{3}
           ,'{4}'
           ,'0'
           ,'admin'
           ,getdate()
           ,null
           ,null
           ,null
           ,null
           ,null
           ,'{5}'
           ,null
           ,null
           ,null
           ,null);";
                sql = string.Format(sql, billNo, item, row.qoor, 0, "PCS", row.revi);
                cmd.Append(sql);
            }
            if (cmd.Length > 0)
                SqlDBHelper.ExcuteSQL105(cmd.ToString());
        }

        //分包C6583R曲靖处理
        public void DoFBC6583R(List<tddev106> tddev106s)
        {
            //分包C6583R
            var dt = tddev106s.FindAll(x => x.stco == "C6583R");
            StringBuilder cmd = new StringBuilder();
            foreach (var row in dt)
            {
                string sql = "";
                sql = "select 'A0204' +  CONVERT(varchar(8), GETDATE(), 12)+right('00000'+ltrim(NEXT VALUE FOR SEQ_INSTOCK),5)";
                var billNo = SqlDBHelper.GetObject(sql).ToString();

                var item = row.item;

                //插入分包采购
                sql = @"INSERT INTO [dbo].[wms_T_InStockRM]
           ([BillNo]
           ,[BillType]
           ,[RelatedBill]
           ,[Supplier]
           ,[Storage_SN]
           ,[Status]
           ,[Remark]
           ,[Creator]
           ,[CreateTime]
           ,[Updator]
           ,[UpdateTime]
           ,[Ext1]
           ,[Ext2]
           ,[Ext3]
           ,[SupplierName]
           ,[Updator2]
           ,[UpdateTime2]
           ,[Updator3]
           ,[UpdateTime3]
           ,[Updator4]
           ,[UpdateTime4]
           ,[SHStorage_SN])
     VALUES
           ('{0}'
           ,'分包入库'
           ,'C6583R'
           ,'SAA'
           ,'RMWHS'
           ,'0'
           ,null
           ,'admin'
           ,getdate()
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null
           ,null);";
                sql = string.Format(sql, billNo);
                cmd.Append(sql);
                sql = @"INSERT INTO [dbo].[wms_T_InStockRMRows]
           ([BillNo]
           ,[RelatedBill]
           ,[PartNumber]
           ,[RowNo]
           ,[OrderNo]
           ,[PlanQty]
           ,[RealQty]
           ,[Unit]
           ,[Status]
           ,[Creator]
           ,[CreateTime]
           ,[Updator]
           ,[UpdateTime]
           ,[Ext1]
           ,[Ext2]
           ,[Ext3]
           ,[Version]
           ,[WaitRemark]
           ,[ToolRemark]
           ,[ASN]
           ,[LotNo])
     VALUES
           ('{0}'
           ,'C6583R'
           ,'{1}'
           ,1
           ,1
           ,{2}
           ,{3}
           ,'{4}'
           ,'0'
           ,'admin'
           ,getdate()
           ,null
           ,null
           ,null
           ,null
           ,null
           ,'{5}'
           ,null
           ,null
           ,null
           ,null);";
                sql = string.Format(sql, billNo, row.item, row.qoor, 0, "PCS", row.revi);
                cmd.Append(sql);
            }
            if (cmd.Length > 0)
                SqlDBHelper.ExcuteSQL102(cmd.ToString());
        }

        public void TransferSync()
        {
            Tddev106DAL tddev106DAL = new Tddev106DAL();
            WMS_T_OutStorckRMRowDetailsDAL wMS_T_OutStorckRMRowDetailsDAL = new WMS_T_OutStorckRMRowDetailsDAL();

            var ListTddev106 = tddev106DAL.GetTransferSyncData();

            if (ListTddev106.Count > 0)
            {
                if (new BaseOracleDAL<tddev106>().InsertBatch(ListTddev106) > 0) //同步
                {
                    DoFBV5951S(ListTddev106);
                    DoFBC6583R(ListTddev106);
                    tddev106DAL.UpdateTransferStatus(ListTddev106); //更新本地
                }
            }
        }

        public void RMOutStatuOverSync()    //更新完成状态
        {
            Tddev106DAL tddev106DAL = new Tddev106DAL();
            tddev106DAL.CloseYwHead();
            WMS_T_InStockRowsDAL wMS_M_InStockDAL = new WMS_T_InStockRowsDAL();

            var StatusOver = tddev106DAL.GetOutStatuOverSyncData();

            if (StatusOver != null && StatusOver.Rows.Count > 0)
            {
                var result = wMS_M_InStockDAL.UpdateOutStatus(StatusOver);
                if (result > 0) //同步
                {
                    tddev106DAL.UpdateOutStatusOver(StatusOver); //更新本地
                }
            }
        }

        public void SH_PO_Adjust(tddev102 tdd)
        {
            if (tdd.cwar == "RMD3X" || tdd.cwar == "RMD4X")
            {
                //移库对象
                tddev106 t106 = new tddev106();
                t106.guid = tdd.Guid;
                t106.WMSNO = tdd.WMSPO;
                t106.orno = tdd.orno;
                t106.pono = tdd.pono;
                t106.seri = "WH5";
                t106.otyp = "TWH";
                t106.item = tdd.item;
                t106.revi = " ";
                t106.sfco = tdd.cwar;
                t106.sflo = " ";
                t106.stco = "RMWHS";
                t106.stlo = " ";
                t106.qoor = Convert.ToDecimal(tdd.reci);
                t106.host = 1;
                t106.mess = " ";
                t106.edte1 = tdd.edte1;
                t106.wdte1 = tdd.wdte1;
                t106.edte2 = tdd.edte1;
                t106.REFCNTD = 0;
                t106.REFCNTU = 0;
                t106.epon = 0;
                t106.seqn = 0;

                t106.Remark1 = " ";
                t106.Remark2 = " ";
                t106.Remark3 = " ";
                t106.Remark4 = " ";
                t106.Remark5 = " ";

                new BaseOracleDAL<tddev106>().Insert(t106);
            }
        }
        //***END***

    }
}
